<?php
require_once('../../config.php');
require_once('security.php');    
require_once('../functions/getIDs.php');
  
   // Retrieve the file contents
  $page = $_GET['page'];
  $filename = "../textfiles/$page.txt";
  if (file_exists($filename)) 
  {
    // Get the contents
    $file_contents = file_get_contents($filename);
    $file_contents = htmlspecialchars($file_contents);
    $splitcontents = explode("\n", $file_contents);
  }

  // Apend
  if (isset($_POST['submit1'])) { // Handle the form.
    $content = getAvailableID() . "|" . $_POST['name'] . "|" . $_POST['cost'];
    $content = stripslashes($content);
    $page = $_POST['page'];
    $filename = '../textfiles/' . $page . '.txt';
    $fp = fopen($filename, "a") or die("Couldn't open $filename.");
    fwrite($fp, "\r\n$content");
    fclose($fp);
    $response = "The content was added.";
  }


?>

<?php $thisPage = "write"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Write page content</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">

<?php include_once('navigation.php'); ?>

<?php echo 'Category = ' . $page . '<br />'; ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Item Name:<br />
<input type="text" name="name" id="name" size="40" value="<?php if (isset($_GET['name'])) { echo $_GET['name']; } else { echo $name; } ?>" maxlength="60" /><br />
Item Price (With "$")<br />
<input type="text" name="cost" id="cost" size="10" value="<?php if (isset($_GET['cost'])) { echo $_GET['cost']; } else { echo $cost; } ?>" maxlength="10" /><br />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="submit" name="submit1" value="Save Item">
</form>

<?php echo $response; ?>

<?php

  if ($page == "index") {
    echo '<a href="' . $wwwroot. '/cms">View &raquo;</a>';
  } else {
    echo '<a href="'. $wwwroot . '/cms/' . $page . '.php">View &raquo;</a>';
  }
?>
<?php include_once('list-items.php'); ?>
<div style="clear: both;"></div>
</div>

</body>
</html>
